package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.domain.R;
import org.example.domain.User;
import org.example.service.UserService;
import org.example.service.impl.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpServletRequest request) {
        String email = user.getPhone();
//        MailService mailService = new MailService();
//        mailService.sendMail(phone);
        MimeMessage message = mailSender.createMimeMessage();

        String code = String.valueOf(new Random().nextInt(899999) + 100000);
//        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
//        ops.set(email, code, 10, TimeUnit.MINUTES);
        // 创建一个MimeMessage对象
        System.out.println(code);
        HttpSession session = request.getSession();
        session.setAttribute("code", code);
        MimeMessageHelper helper;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setFrom("1026155489@qq.com"); // 发件人邮箱
            helper.setTo(email); // 收件人邮箱
            helper.setSubject("验证码"); // 邮件主题
            helper.setText("您的验证码为：" + code); // 邮件内容

            mailSender.send(message); // 发送邮件
            return R.success("验证码已发送");
        } catch (MessagingException e) {
            e.printStackTrace();
            return R.error("发送失败，请检查邮箱地址是否正确");
        }

    }

    @PostMapping("/login")
    public R<String> login(@RequestBody Map map, HttpServletRequest request) {
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getPhone, phone);
        HttpSession session = request.getSession();
        Object code1 = session.getAttribute("code");

        if (((String) code1).equals(code)) {


            User one = userService.getOne(userLambdaQueryWrapper);
            if (one == null) {
                one = new User();
                one.setPhone(phone);
                one.setName(phone);
                one.setStatus(1);
                userService.save(one);
            }
            session.setAttribute("user", one.getId());
            return R.success("ok");
        }
        return R.error("error");
    }

    @PostMapping("/loginout")
    public R<String> loginout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        System.out.println("ok");
        return R.success("ok");
    }

}
